﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frm量具詳細 : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string flag;
        string tb1,tb2, tb3, tb4, tb5, tb6, tb7, tb8, tb9, tb10, tb11, tb12, tb13, tb14, tb15, tb16, tb17, tb18, tb19, tb20;
        string dt1;
		object img;
        string str = null;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frm量具詳細(string flag, string tb1, string tb2, string tb3, string tb4, string tb5,
			string tb6,
			string tb7,
			string tb8,
			string tb9,
			string tb10,
			string tb11,
			string tb12,
			string tb13,
			string tb14,
			string tb15,
			string tb16,
			string tb17,
			string tb18,
			string tb19,
			string dt1,
            object img,
            string tb20)
		{
			InitializeComponent();
			
            this.flag = flag;
            this.tb1 = tb1;
            this.tb2 = tb2;
            this.tb3 = tb3;
            this.tb4 = tb4;
            this.tb5 = tb5;
			
			this.tb6 = tb6;
            this.tb7 = tb7;
            this.tb8 = tb8;
            this.tb9 = tb9;
            this.tb10 = tb10;
			this.tb11 = tb11;
            this.tb12 = tb12;
            this.tb13 = tb13;
            this.tb14 = tb14;
            this.tb15 = tb15;
			this.tb16 = tb16;
			this.tb17 = tb17;
			this.tb18 = tb18;
			this.tb19 = tb19;
			this.dt1 = dt1;
            //this.dt2 = dt2;
            //this.dt3 = dt3;
			this.img = img;
            this.tb20 = tb20;
        }

        private void frm量具詳細_Load(object sender, EventArgs e)
        { 
            //LoadCommobox();
            //txt量具狀態.Visible = false;
            label21.Visible = false;
            txt量具狀態.Visible = false;
            if (flag == "1") { btn_Save_Edit.Text = "存檔"; txt量具狀態.Text = "库藏"; txt量具狀態.Enabled = false; }
            if (flag == "2") 
            {     
                btn_Save_Edit.Text = "修改";
                //txt量具編號.Enabled = false;
                txt量具狀態.Enabled = false;
                label21.Enabled = false;
                str = tb1;
                //dateTimePicker_購買日期.Enabled = false;
                //dateTimePicker_入庫日期.Enabled = false;

                txt量具編號.Text = tb1;//tb1 0
                txt量具狀態.Text = tb2;//cb1 1
                txt量具名稱.Text = tb3; //tb3 2
                txt材料編號.Text = tb4; //tb4 3
                txt校正類別.Text = tb5; //cb2  4
                txt量具類別.Text = tb6; //cb3 5
                txt規格型號.Text = tb7; //tb4 6
                txt廠牌.Text = tb8; //cb4 7
                txt存放地點.Text = tb9; //cb5 8
                txt管理類別.Text = tb10; //tb5 9
                dateTimePicker_出廠日期.Text = dt1; //dt1 10
                txt測量範圍.Text = tb11; //tb6 11
                //dateTimePicker_購買日期.Text = dt2; //dt2 12
                txt請購人員.Text = tb12; //cb6 13
                txt外檢機構.Text = tb13; //tb7 14
                txt出廠編號.Text = tb14; //tb8 15
                txt測量精度.Text = tb15; //tb9 16
                //dateTimePicker_入庫日期.Text = dt3; //dt3 17
                txt內檢週期.Text = tb16; //tb10 18
                txt外檢週期.Text = tb17; //tb11 19
				//txt加附.Text = tb18; //url
                txt_note.Text = tb18; //tb12 21
                if (string.IsNullOrEmpty(img.ToString())) Picture_量具.Image = null;
                else Picture_量具.Image = byteArraytoImage((byte[]) img); //img1 20
                txt数量.Text = tb20;

            }
            txt加附.Enabled = false;
            Load量具詳細();
            //SqlConnection con = new SqlConnection(connectionString);
            //try
            //{

            //}
            //catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

        public void ResetText量具詳細()
        {
            txt_note.ResetText();
            txt內檢週期.ResetText();
            txt出廠編號.ResetText();
            txt外檢機構.ResetText();
            txt外檢週期.ResetText();
            txt存放地點.Text = "";
            txt廠牌.Text = "";
            txt材料編號.ResetText();
            txt校正類別.ResetText();
            txt測量範圍.ResetText();
            txt測量精度.ResetText();
            txt管理類別.ResetText();
            txt規格型號.ResetText();
            txt請購人員.Text = "";
            txt量具名稱.ResetText();
            txt量具編號.ResetText();
            txt量具類別.Text="";
            //dateTimePicker_入庫日期.ResetText();
            dateTimePicker_出廠日期.ResetText();
            //dateTimePicker_購買日期.ResetText();
            txt量具狀態.Text="库藏";
            Picture_量具.ResetText();
            txt加附.ResetText();
            txt数量.ResetText();
        }


        public void Load量具詳細()
        {
            //ResetText量具詳細();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                frm量具台帳 frm = new frm量具台帳(b量具Search, b量具Insert, b量具Edit, b量具Del);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_量具訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                frm.Load量具訊息();
                //dataGV校正類別訊息.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                //dataGV校正類別訊息.DataSource = dt;
                //PublicClass.setRowNumber(dataGV校正類別訊息);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void LoadCommobox()
        {
            ////cmb量具狀態.DropDownStyle = ComboBoxStyle.DropDownList;
            ////Load CmbTable-cmb量具類別
            //SqlConnection con = new SqlConnection(connectionString);
            //string sql = "select * from 量具類別訊息";
            //SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            //DataTable dt = new DataTable();
            //ad.Fill(dt);
            //txt量具類別.DataSource = dt;
            //txt量具類別.DisplayMember = "量具類別名稱";
            //txt量具類別.ValueMember = "量具類別代號";

            ////Load CmbTable-cmb廠牌
            //string sql2 = "select * from 廠牌訊息";
            //SqlDataAdapter ad2 = new SqlDataAdapter(sql2, con);
            //DataTable dt2 = new DataTable();
            //ad2.Fill(dt2);
            //txt廠牌.DataSource = dt2;
            //txt廠牌.DisplayMember = "廠牌名稱";
            //txt廠牌.ValueMember = "廠牌代號";

            ////Load CmbTable-cmb存放地點
            //string sql3 = "select * from 存放地點訊息";
            //SqlDataAdapter ad3 = new SqlDataAdapter(sql3, con);
            //DataTable dt3 = new DataTable();
            //ad3.Fill(dt3);
            //txt存放地點.DataSource = dt3;
            //txt存放地點.DisplayMember = "存放地點";
            //txt存放地點.ValueMember = "存放地點代號";

            ////Load CmbTable-cmb校正類別
            //string sql4 = "select * from 校正類別訊息";
            //SqlDataAdapter ad4 = new SqlDataAdapter(sql4, con);
            //DataTable dt4 = new DataTable();
            //ad4.Fill(dt4);
            //txt校正類別.DataSource = dt4;
            //txt校正類別.DisplayMember = "校正類別名稱";
            //txt校正類別.ValueMember = "校正類別代號";

            ////Load CmbTable-cmb請購人員
            //string sql5 = "select * from 請購人員訊息";
            //SqlDataAdapter ad5 = new SqlDataAdapter(sql5, con);
            //DataTable dt5 = new DataTable();
            //ad5.Fill(dt5);
            //txt請購人員.DataSource = dt5;
            //txt請購人員.DisplayMember = "請購人員姓名";
            //txt請購人員.ValueMember = "請購人員代號";

        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetText量具詳細();
        }
        
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //string loadfrm;
        private void btn_Save_Edit_Click(object sender, EventArgs e)
        {
            if (flag == "1")
            {
                btn_Save_Edit.Text = "存檔";
                try
                {
                    if (txt量具編號.Text == "")
                    {
                        MessageBox.Show("請您輸入量具編號", "錯誤", MessageBoxButtons.OK);
                        txt量具編號.Focus();
                        return;
                    }

                    if (txt量具狀態.Text == "")
                    {
                        MessageBox.Show("請您輸入量具狀態", "錯誤", MessageBoxButtons.OK);
                        txt量具狀態.Focus();
                        return;
                    }
                    if (txt数量.Text == "")
                    {
                        MessageBox.Show("請您輸入数量", "錯誤", MessageBoxButtons.OK);
                        txt数量.Focus();
                        return;
                    }
                    DialogResult answer = MessageBox.Show("您確定要新增" + int.Parse(txt数量.Text) + "量具", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        //bat dau insert vao database
                        SqlCommand cmd_count = new SqlCommand();
                        cmd_count.CommandText = "sp_量具訊息_count";
                        cmd_count.CommandType = CommandType.StoredProcedure;
                        cmd_count.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                        //cmd_count.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text);
                        cmd_count.Connection = con;
                        con.Open();
                        var icount = cmd_count.ExecuteScalar();
                        if (int.Parse(icount.ToString()) == 0)
                        {
                            //con.Open();
                            string sp_insert = "sp_量具訊息_新增";
                            SqlCommand cmd = new SqlCommand(sp_insert, con);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                            cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                            cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                            cmd.Parameters.AddWithValue("@材料編號", txt材料編號.Text);
                            cmd.Parameters.AddWithValue("@校正類別代號", txt校正類別.Text.ToString());
                            cmd.Parameters.AddWithValue("@量具類別代號", txt量具類別.Text.ToString());
                            cmd.Parameters.AddWithValue("@規格型號", txt規格型號.Text);
                            cmd.Parameters.AddWithValue("@廠牌代號", txt廠牌.Text.ToString());
                            cmd.Parameters.AddWithValue("@存放地點代號", txt存放地點.Text.ToString());
                            cmd.Parameters.AddWithValue("@管理類別", txt管理類別.Text);
                            cmd.Parameters.AddWithValue("@出廠日期", DateTime.Parse(dateTimePicker_出廠日期.Text));
                            cmd.Parameters.AddWithValue("@測量範圍", txt測量範圍.Text);
                            //cmd.Parameters.AddWithValue("@購買日期", (DateTime)dateTimePicker_購買日期.MaxDate);
                            cmd.Parameters.AddWithValue("@請購人員代號", txt請購人員.Text.ToString());
                            cmd.Parameters.AddWithValue("@外檢機構", txt外檢機構.Text);
                            cmd.Parameters.AddWithValue("@出廠編號", txt出廠編號.Text);
                            cmd.Parameters.AddWithValue("@測量精度", txt測量精度.Text);
                            //cmd.Parameters.AddWithValue("@入庫日期", (DateTime)dateTimePicker_入庫日期.MaxDate);
                            cmd.Parameters.AddWithValue("@內檢週期", txt內檢週期.Text);
                            cmd.Parameters.AddWithValue("@外檢週期", txt外檢週期.Text);
                            if (txt加附.Text.ToString() != "")
                            {
                                byte[] imageData = ReadFile(txt加附.Text.ToString());//ReadFile tu viet o ben duoi
                                cmd.Parameters.AddWithValue("@照片", (object)imageData);
                            }
                            cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                            cmd.Parameters.AddWithValue("@数量", txt数量.Text);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("新增成功");
                            Load量具詳細();
                            con.Close();
                            this.Close();
                            //frm量具台帳 frm = new frm量具台帳();
                            //frm.Show();
                        }
                        else
                        {
                            //automation fill other filed
                            string sp_update = "sp_量具訊息_更新_+n";
                            SqlCommand cmd1 = new SqlCommand(sp_update, con);
                            cmd1.CommandType = CommandType.StoredProcedure;
                            cmd1.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                            cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                            cmd1.Parameters.AddWithValue("@n", txt数量.Text);
                            cmd1.ExecuteNonQuery();
                            MessageBox.Show("更新成功");
                            Load量具詳細();
                            con.Close();
                            this.Close();
                        }
                    }
                    else return;
                    con.Close();
                    return;
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
                
            }

            if (flag == "2")
            {
                btn_Save_Edit.Text = "修改";
                txt量具編號.Enabled = false;
                txt量具狀態.Enabled = false;
                //dateTimePicker_購買日期.Enabled = false;
                //dateTimePicker_入庫日期.Enabled = false;
                try
                {
                    if (txt量具編號.Text == "") { MessageBox.Show("請您輸入量具編號更新"); }
                    else
                    {
                        con.Open();
                        SqlCommand cmd;
                        String sql = "SELECT COUNT(*) FROM 量具訊息 WHERE 量具編號='" + str + "'";
                        cmd = new SqlCommand(sql, con);
                        if ((int)cmd.ExecuteScalar() > 0)
                        {
                            string sp_update = "sp_量具訊息_更新";
                            cmd = new SqlCommand(sp_update, con);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@量具編號", str);
                            cmd.Parameters.AddWithValue("@量具編號2", txt量具編號.Text);
                            cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                            cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                            cmd.Parameters.AddWithValue("@材料編號", txt材料編號.Text);
                            cmd.Parameters.AddWithValue("@校正類別代號", txt校正類別.Text.ToString());
                            cmd.Parameters.AddWithValue("@量具類別代號", txt量具類別.Text.ToString());
                            cmd.Parameters.AddWithValue("@規格型號", txt規格型號.Text);
                            cmd.Parameters.AddWithValue("@廠牌代號", txt廠牌.Text.ToString());
                            cmd.Parameters.AddWithValue("@存放地點代號", txt存放地點.Text.ToString());
                            cmd.Parameters.AddWithValue("@管理類別", txt管理類別.Text);
                            cmd.Parameters.AddWithValue("@出廠日期", DateTime.Parse(dateTimePicker_出廠日期.Text));
                            cmd.Parameters.AddWithValue("@測量範圍", txt測量範圍.Text);
                            //cmd.Parameters.AddWithValue("@購買日期", (DateTime)dateTimePicker_購買日期.MaxDate);
                            cmd.Parameters.AddWithValue("@請購人員代號", txt請購人員.Text.ToString());
                            cmd.Parameters.AddWithValue("@外檢機構", txt外檢機構.Text);
                            cmd.Parameters.AddWithValue("@出廠編號", txt出廠編號.Text);
                            cmd.Parameters.AddWithValue("@測量精度", txt測量精度.Text);
                            //cmd.Parameters.AddWithValue("@入庫日期", (DateTime)dateTimePicker_入庫日期.MaxDate);
                            cmd.Parameters.AddWithValue("@內檢週期", txt內檢週期.Text);
                            cmd.Parameters.AddWithValue("@外檢週期", txt外檢週期.Text);
                            if (txt加附.Text.ToString() != "")
                            {
                                byte[] imageData = ReadFile(txt加附.Text.ToString());//ReadFile tu viet o ben duoi
                                cmd.Parameters.AddWithValue("@照片", (object)imageData);
                            }
                            cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                            cmd.Parameters.AddWithValue("@数量", txt数量.Text);
                            cmd.ExecuteNonQuery();
                            Load量具詳細();
                            MessageBox.Show("更新成功");
                            con.Close();
                            this.Close();
                            //frm量具台帳 frm = new frm量具台帳();
                            //frm.Show();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Error! '" + txt量具編號.Text.ToString() + "' 不由來");
                            con.Close();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Load量具詳細();
                    Close();
                    return;
                }
                this.Close();
            }
        }

        private void btn加附_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Title = "開啟照片";
                dialog.Filter = "照片當 (*.jpg, *.jpeg, *.jpe, *.bmp, *.png, *.gif, *.ico) | *.jpg; *.jpeg; *.jpe; *.bmp; *.png; gif; *.ico | All File|*.*"; ;//Bitmap, Icon,GIF,bmp,ico,gif,jpeg
                var result = dialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    string url = dialog.FileName;
                    txt加附.Text = dialog.FileName;
                    Image img = Image.FromFile(txt加附.Text.ToString());
                    //PhucTG Viet Resize
                    //Lay Size anh ban dau
                    int originalW = img.Width;
                    int originalH = img.Height;
                    //size anh moi theo ti le
                    int resizedW = (int)(180);
                    int resizedH = (int)(160);
                    //tao 1 anh bitmap moi theo kich thuoc tren
                    Bitmap bmp = new Bitmap(resizedW, resizedH);
                    //ve lai anh ban dau len bmp theo kich thuoc moi
                    using (Graphics g = Graphics.FromImage(bmp))
                        g.DrawImage(img, 0, 0, resizedW, resizedH);
                    //giai phong tai nguyen ma graphic dang giu
                    //g.Dispose();
                    //return the image
                    //return (Image)bmp;
                    Picture_量具.Image = bmp;
                    //Picture_量具.Image = img;
                    //End PhucTG Viet Resize
                }
                else return;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }
        public Image byteArraytoImage(byte[] filesbyte)
        {
            using (System.IO.MemoryStream filestream = new System.IO.MemoryStream(filesbyte))
            {
                return Image.FromStream(filestream);
            }
        }
        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes 
            //to read from file.
            //In this case we want to read entire file. 
            //So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);

            return data;
        }

        private void btn請購人員Search_Click(object sender, EventArgs e)
        {
            FrmSearchqinggourenyuan frm = new FrmSearchqinggourenyuan(txt請購人員);
            frm.ShowDialog();
        }

        private void txt請購人員_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 請購人員訊息 where 請購人員代號=N'{0}'", txt請購人員.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName請購人員.Text = "";
                else lbName請購人員.Text = temp;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch校正類別_Click(object sender, EventArgs e)
        {
            FrmSearchjiaozhengleibie frm = new FrmSearchjiaozhengleibie(txt校正類別);
            frm.ShowDialog();
        }

        private void txt校正類別_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 校正類別訊息 where 校正類別代號=N'{0}'", txt校正類別.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName校正類別.Text = "";
                else lbName校正類別.Text = temp;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt量具類別_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具類別訊息 where 量具類別代號=N'{0}'", txt量具類別.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName量具類別.Text = "";
                else lbName量具類別.Text = temp;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt廠牌_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 廠牌訊息 where 廠牌代號=N'{0}'", txt廠牌.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName廠牌.Text = "";
                else lbName廠牌.Text = temp;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt存放地點_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 存放地點訊息 where 存放地點代號=N'{0}'", txt存放地點.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName存放地點.Text = "";
                else lbName存放地點.Text = temp;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch量具類別_Click(object sender, EventArgs e)
        {
            frmSearchLiangjuleibie frm = new frmSearchLiangjuleibie(txt量具類別);
            frm.ShowDialog();
        }

        private void btnSearch廠牌_Click(object sender, EventArgs e)
        {
            FrmSearchChangpaixunxi frm = new FrmSearchChangpaixunxi(txt廠牌);
            frm.ShowDialog();
        }

        private void btn存放地點_Click(object sender, EventArgs e)
        {
            FrmSearchCunfangdidian frm = new FrmSearchCunfangdidian(txt存放地點);
            frm.ShowDialog();
        }

        public void disnabletb()
        {
            btn加附.Enabled = false;
            btnSearch量具類別.Enabled = false;
            btnSearch廠牌.Enabled = false;
            btn存放地點.Enabled = false;
            btn請購人員Search.Enabled = false;
            btnSearch校正類別.Enabled = false;
            txt数量.Enabled = true;
            txt量具名稱.Enabled = false;
            txt材料編號.Enabled = false;
            txt校正類別.Enabled = false;
            txt量具狀態.Enabled = false;
            txt量具類別.Enabled = false;
            txt規格型號.Enabled = false;
            txt廠牌.Enabled = false;
            txt存放地點.Enabled = false;
            txt管理類別.Enabled = false;
            dateTimePicker_出廠日期.Enabled = false;
            txt測量範圍.Enabled = false;
            txt請購人員.Enabled = false;
            txt外檢機構.Enabled = false;
            txt出廠編號.Enabled = false;
            txt測量精度.Enabled = false;
            txt內檢週期.Enabled = false;
            txt外檢週期.Enabled = false;
            txt_note.Enabled = false;
            Picture_量具.Enabled = false;
        }

        public void Enabletb()
        {
            label21.Visible = false;
            txt量具狀態.Visible = false;
            btn加附.Enabled = true;
            btnSearch量具類別.Enabled = true;
            btnSearch廠牌.Enabled = true;
            btn存放地點.Enabled = true;
            btn請購人員Search.Enabled = true;
            btnSearch校正類別.Enabled = true;
            txt数量.Enabled = true;
            txt量具名稱.Enabled = true;
            txt材料編號.Enabled = true;
            txt校正類別.Enabled = true;
            txt量具狀態.Enabled = false;
            txt量具類別.Enabled = true;
            txt規格型號.Enabled = true;
            txt廠牌.Enabled = true;
            txt存放地點.Enabled = true;
            txt管理類別.Enabled = true;
            dateTimePicker_出廠日期.Enabled = true;
            txt測量範圍.Enabled = true;
            txt請購人員.Enabled = true;
            txt外檢機構.Enabled = true;
            txt出廠編號.Enabled = true;
            txt測量精度.Enabled = true;
            txt內檢週期.Enabled = true;
            txt外檢週期.Enabled = true;
            txt_note.Enabled = true;
            Picture_量具.Enabled = true;
        }

        private void btn_CheckMa_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                string strcmd = "select * from 量具訊息,量具類別訊息, 校正類別訊息, 存放地點訊息 where 量具編號=" + "N'" + txt量具編號.Text + "' and 量具訊息.量具類別代號=量具類別訊息.量具類別代號 and 量具訊息.校正類別代號=校正類別訊息.校正類別代號 and 量具訊息.存放地點代號=存放地點訊息.存放地點代號";
                SqlDataAdapter da1 = new SqlDataAdapter(strcmd, con);
                DataTable dt1 = new DataTable();
                da1.Fill(dt1);
                //get data from datatable
                if (dt1.Rows.Count == 0)
                {
                    MessageBox.Show("這是新量具，請輸入其他訊息");
                    txt量具名稱.Focus();
                }
                else
                {
                    disnabletb();
                    txt量具名稱.Text = dt1.Rows[0]["量具名稱"].ToString();
                    txt材料編號.Text = dt1.Rows[0]["材料編號"].ToString();
                    txt校正類別.Text = dt1.Rows[0]["校正類別代號"].ToString();
                    txt量具狀態.Text = dt1.Rows[0]["量具狀態"].ToString();
                    txt量具類別.Text = dt1.Rows[0]["量具類別代號"].ToString();
                    txt規格型號.Text = dt1.Rows[0]["規格型號"].ToString();
                    //chb停止供貨.Checked = (bool)dt1.Rows[0]["廠牌代號"];
                    txt廠牌.Text = dt1.Rows[0]["廠牌代號"].ToString();
                    txt存放地點.Text = dt1.Rows[0]["存放地點代號"].ToString();
                    txt管理類別.Text = dt1.Rows[0]["管理類別"].ToString();
                   DateTime.Parse(dateTimePicker_出廠日期.Text = dt1.Rows[0]["出廠日期"].ToString());
                   txt測量範圍.Text = dt1.Rows[0]["測量範圍"].ToString();
                   txt請購人員.Text = dt1.Rows[0]["請購人員代號"].ToString();
                   txt外檢機構.Text = dt1.Rows[0]["外檢機構"].ToString();
                   txt出廠編號.Text = dt1.Rows[0]["出廠編號"].ToString();
                   txt測量精度.Text = dt1.Rows[0]["測量精度"].ToString();
                   txt內檢週期.Text = dt1.Rows[0]["內檢週期"].ToString();
                   txt外檢週期.Text = dt1.Rows[0]["外檢週期"].ToString();
                   txt_note.Text = dt1.Rows[0]["備註"].ToString();
                   //txt数量.Text = dt1.Rows[0]["数量"].ToString();
                   txt数量.Text = "";
                    Picture_量具.Image = (string.IsNullOrEmpty(txt加附.Text) ? null : byteArraytoImage((byte[])dt1.Rows[0]["照片"]));
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt量具編號_TextChanged(object sender, EventArgs e)
        {
            //ResetText量具詳細();
            Enabletb();
        }

        private void txt数量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
    }
}
